package com.example.ware.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.ware.annotation.Token;
import com.example.ware.dao.DeclareMapper;
import com.example.ware.model.Spage.ShowPage;
import com.example.ware.model.po.Admin;
import com.example.ware.model.po.Classify;
import com.example.ware.model.po.Declares;
import com.example.ware.model.po.Employees;
import com.example.ware.model.responseData.ResponseData;
import com.example.ware.model.vo.DeclareVo;
import com.example.ware.service.ClassifyService;
import com.example.ware.service.DeclareService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 *提交事务
 *  */
@RestController
@CrossOrigin
@RequestMapping("/declare")
public class DeclareController {

    @Autowired
    private DeclareService delcareService;

    @Autowired
    private ClassifyService classifyService;

    @Resource
    private DeclareMapper declareMapper;

    @Resource
    RedisTemplate redisTemplate;


    /**
*提交申请（/submit）
*  */
//    提交申请的接口
    @PostMapping("/submit")
    public ResponseData submit(@RequestBody Declares delcare, @RequestHeader String token){



        Employees e = (Employees) redisTemplate.opsForValue().get("TOKEN_" + token);
        System.out.println("1111111");
        System.out.println(e);
        delcare.setEid(e.getId());
        Date date=new Date();
        delcare.setTime(date);
        delcare.setId(UUID.randomUUID().toString().replaceAll("-", ""));
        delcare.setAudit(null);
        delcare.setAid(null);
        delcare.setIsuse("否");
        delcare.setIssubmit("否");
        delcare.setStatus(0);



        boolean save = delcareService.save(delcare);

        if(save){
            return new ResponseData(null, true, "申请成功", 200);
        }else{
            return new ResponseData(null, false, "申请失败", 201);
        }
    }



    /**
     *获取申请列表（总管理员+仓库专员）
     *  */
    @GetMapping("/list")
    @Token
    public ResponseData getDelcareList(@RequestHeader String token, String name,String sort, Integer status,
                                       String id,String cid,@RequestParam(defaultValue = "1") Integer page,@RequestParam(defaultValue = "3") Integer pageSize ){

        Admin admin = (Admin)redisTemplate.opsForValue().get("Admin_" + token);
        String wid;
        if(admin.getAuth()==1){
            wid="";
        }else{
            wid=admin.getWid();
        }

        ShowPage showPage = new ShowPage();
        Integer pageBegin = (page-1) * pageSize;
        showPage.setPageNum(page);
        //总数据条数
        showPage.setSize(declareMapper.getDelcareAll(status,wid,name,cid,id).size());
        //每页数据条数
        showPage.setPageSize(pageSize);
        //总页数
        Integer div = showPage.getSize()/pageSize;
        Integer pages = showPage.getSize() % pageSize == 0 ? div : div + 1;
        showPage.setPages(pages);

        List<DeclareVo> declareVoList= declareMapper.getDelcareList(status,wid,name,cid,sort,pageBegin,pageSize,id);
        showPage.setData(declareVoList);

        if(showPage.getData()==null){
            return new ResponseData(null, false, "出问题了,请联系程序员", 201);
        }else{
            return new ResponseData(showPage, true, "获取成功", 200);
        }
    }

    /**
     审核
     */
    @PutMapping("/audit")
    @Token
    public ResponseData AuditDeclare(@RequestHeader String token,String id,Integer status,String audit,String wid){
        Admin admin = (Admin)redisTemplate.opsForValue().get("Admin_" + token);
        Date date=new Date();
        if(admin.getWid().equals(wid)){
            UpdateWrapper<Declares> uw=new UpdateWrapper();
            uw.eq("id",id).set("status",status).set("audit",audit).set("isuse","否").set("aid",admin.getId()).set("audittime",date);

            boolean i = delcareService.update(uw);

            if(i){
                return new ResponseData(null,true,"审核成功",200);
            }else{
                return new ResponseData(null,false,"审核失败，请联系管理员",201);
            }

        }else{
            return new ResponseData(null,false,"这个仓库不是你管理的",404);
        }
    }


//    获取员工个人的请求接口
    @GetMapping("/emp")
    public ResponseData getDeclInfo(String start,String end,
            @RequestHeader String token,@RequestParam(defaultValue = "1") Integer page,@RequestParam(defaultValue = "3") Integer pageSize){

        Employees e = (Employees)redisTemplate.opsForValue().get("TOKEN_" + token);

        if(e==null){
            return new ResponseData(null,false,"您非公司员工",500);
        }
        String name=e.getUsername();


        ShowPage showPage = new ShowPage();
        Integer pageBegin = (page-1) * pageSize;
        showPage.setPageNum(page);
        //总数据条数
        showPage.setSize(declareMapper.getDelcareEmListAll(start,end,name).size());
        //每页数据条数
        showPage.setPageSize(pageSize);
        //总页数
        Integer div = showPage.getSize()/pageSize;
        Integer pages = showPage.getSize() % pageSize == 0 ? div : div + 1;
        showPage.setPages(pages);

        List<DeclareVo> declareVoList= declareMapper.getDelcareEmList(start,end,name,pageBegin,pageSize);
        showPage.setData(declareVoList);

        if(showPage.getData()==null){
            return new ResponseData(null, false, "出问题了,请联系程序员", 201);
        }else{
            return new ResponseData(showPage, true, "获取成功", 200);
        }
    }


//    根究id查询
    @GetMapping("/byid")
    public ResponseData getById(String id,@RequestHeader String token){
        Employees e = (Employees)redisTemplate.opsForValue().get("TOKEN_" + token);

        if(e==null){
            return new ResponseData(null,false,"您非公司员工",500);
        }


        DeclareVo one = declareMapper.getOne(id);

        if(one==null){
            return new ResponseData(null,false,"获取失败",201);
        }else{
            return new ResponseData(one,true,"获取成功",200);
        }
    }

}
